package com.xinheyun.demo;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @description: TODO
 * @author: Sarah
 * @modified By: Sarah
 * @date: Created in 2021/8/7 10:57
 * @version:v1.0
 */
public class leedcode020 {


    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.isValid("["));

    }


}

class Solution {


    public boolean isValid(String s) {
        HashMap<Character, Character> hashMap = new HashMap<Character, Character>();
        hashMap.put('[', ']');
        hashMap.put('(', ')');
        hashMap.put('{', '}');
        Deque<Character> deque1 = new ArrayDeque();
        for (int i = 0; i < s.length(); i++) {
            // System.out.println(s.charAt(i));
            if (hashMap.containsKey(s.charAt(i))) {
                deque1.addFirst(s.charAt(i));
            }
            if (hashMap.containsValue(s.charAt(i))) {
                if(deque1.isEmpty()){
                    return false;
                }
                Character character = deque1.removeFirst();
                if (s.charAt(i) != hashMap.get(character)) {
                    return false;
                }
            }
        }
        if (!deque1.isEmpty()) {
            return false;
        }
        return true;
    }
}